package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.qurey.StrategyQuery;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.DBHepler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
//@Transactional mongodb事务必须复制集方式
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IDestinationService destinationService;


    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        repository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        repository.save(strategy);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return repository.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        Query query = new Query();

        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }

        if(StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }

        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        return DBHepler.query(template,pageable,query, Strategy.class,qo);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        //目的地的id
        //目的地名字
        StrategyCatalog catalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setDestId(catalog.getDestId());
        strategy.setDestName(catalog.getDestName());
        //分类名称
        strategy.setCatalogName(catalog.getName());
        //主题名称
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(strategyTheme.getName());

        //是否国外
        List<Destination> toasts = destinationService.queryToasts(catalog.getDestId());
            if (toasts != null&&toasts.size()>0){
                Destination dest = toasts.get(0);
                if ("中国".equals(dest)){
                    strategy.setIsabroad(Strategy.ABROAD_NO);
                }else {
                    strategy.setIsabroad(Strategy.ABROAD_YES);
                }
            }
        if (StringUtils.hasLength(strategy.getId())){
                //维护不需要修改数据
                //查询
            Strategy temp = this.get(strategy.getId());
                //替换
                strategy.setCreateTime(temp.getCreateTime());
                strategy.setViewnum(temp.getViewnum());
                strategy.setReplynum(temp.getReplynum());
                strategy.setFavornum(temp.getFavornum());
                strategy.setSharenum(temp.getSharenum());
                strategy.setThumbsupnum(temp.getThumbsupnum());
            this.update(strategy);
        }else {
            //创建时间
            strategy.setCreateTime(new Date());
            this.save(strategy);
        }

    }

    @Override
    public List<Strategy> queryByCatalogId(String id) {
        return repository.findByCatalogId(id);
    }

    @Override
    public List<Strategy> queryViewnumByDestId(String destId) {

        PageRequest viewnum = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        List<Strategy>list= repository.findByDestId(destId,viewnum);
        return list;
    }

    @Override
    public void changeState(String id, String state) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("state",state );
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public void updateStatisVo(StrategyStatisVO vo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));

        Update update = new Update();

        update.set("viewnum", vo.getViewnum());
        update.set("replynum", vo.getReplynum());
        update.set("favornum", vo.getFavornum());
        update.set("sharenum", vo.getSharenum());
        update.set("thumbsupnum", vo.getThumbsupnum());
        template.updateMulti(query,update ,Strategy.class );
    }

    @Override
    public List<Strategy> findByDestId(String id) {

        List<Strategy> byDestId = repository.findByDestId(id, null);
        return byDestId;
    }


}
